package com.qdxwx.data.service.teacher;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.mapper.teacher.TeacherMapper;
import com.qdxwx.data.service.manager.ManagerService;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.manager.Manager;
import com.qdxwx.models.teacher.Teacher;
import com.qdxwx.models.teacher.TeacherQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Teacher接口
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class TeacherServiceImpl implements TeacherService {

	@Autowired
	private TeacherMapper teacherMapper;
	@Autowired
	private ManagerService managerService;

	@Override
	public int insert(Teacher teacher) {
		teacher.setUpdateTime(LocalDateTime.now());
		teacher.setCreateTime(LocalDateTime.now());
		return teacherMapper.insertTeacher(teacher);
	}


	@Override
	public int update(Teacher teacher) {
		if (teacher.getId() < 1) return 0;
		teacher.setUpdateTime(LocalDateTime.now());
		return teacherMapper.updateTeacher(teacher);
	}

	@Override
	public Teacher get(int id) {
		if(id < 1) return null;
		return teacherMapper.selectById(id);
	}

	@Override
	public Teacher getByUserId(long userId) {
		if (userId < 1) return null;
		return teacherMapper.selectByUserId(userId);
	}

	@Override
	public boolean checkMobile(String mobile) {
		return getByMobile(mobile) != null;
	}

	@Override
	public Teacher getByMobile(String mobile) {
		if (!TKString.isMobile(mobile)) return null;
		return teacherMapper.selectByMobile(mobile);
	}

	@Override
	public List<Teacher> getList(TeacherQuery query, PageRequest page) {
		return teacherMapper.selectTeacherList(query, page);
	}

	@Override
	public int count(TeacherQuery teacherQuery) {
		return teacherMapper.countTeacher(teacherQuery);
	}

	@Override
	public List<Teacher> getListByCourseId(int courseId) {
		if (courseId < 1) return null;
		return teacherMapper.selectTeacherListByCourseId(courseId);
	}

	@Override
	public List<Teacher> getListByBookId(int bookId) {
		if (bookId < 1) return null;
		return teacherMapper.selectTeacherListByBookId(bookId);
	}

	@Override
	public List<Teacher> getListByCourse(CourseQuery courseQuery, int limit) {
		return teacherMapper.selectTeacherListByCourse(courseQuery, limit);
	}

	@Override
	public String getNamesByBookId(int bookId) {
		List<Teacher> teachers = getListByBookId(bookId);
		if (teachers != null) {
			return teachers.stream().map(Teacher::getName).collect(Collectors.joining(","));
		}
		return "";
	}

	@Override
	public String getNamesByCourseId(int courseId) {
		List<Teacher> teachers = getListByCourseId(courseId);
		if (teachers != null) {
			return teachers.stream().map(Teacher::getName).collect(Collectors.joining(","));
		}
		return "";
	}

	@Override
	public int updateDisabled(Teacher teacher) {
		if (teacher.getId() < 1) return 0;
		if (teacher.getManagerId() > 0) {
			//删除的讲师，对应管理员设置为禁用
			Manager manager = new Manager();
			manager.setManagerId(teacher.getManagerId());
			manager.setIsDisabled(teacher.getIsDisabled());
			managerService.updateDisabled(manager);
		}
		return teacherMapper.updateDisabled(teacher.getId(), teacher.getIsDisabled());
	}

	@Override
	public int updateViewCount(int id) {
		if (id < 1) return 0;
		return teacherMapper.updateViewCount(id);
	}

	@Override
	public int updateLikeCount(int id) {
		if (id < 1) return 0;
		return teacherMapper.updateLikeCount(id);
	}

	@Override
	public int delete(int id) {
		if (id < 1) return 0;
		Teacher teacher = get(id);
		if (teacher == null) {
			return 0;
		}
		if (teacher.getManagerId() > 0) {
			//删除的讲师，对应管理员设置为禁用
			Manager manager = new Manager();
			manager.setManagerId(teacher.getManagerId());
			manager.setIsDisabled(1);
			managerService.updateDisabled(manager);
		}
		return teacherMapper.deleteById(id);
	}

	@Override
	public int updateSort(int id, int sort) {
		if(id < 1) return 0;
		return teacherMapper.updateSort(id, sort);
	}

	@Override
	public int updateCombine(long userId1, long userId2) {
		if(userId1 < 1 || userId2 < 1){
			throw new RuntimeException("更新教师：参数错误");
		}
		return teacherMapper.updateCombine(userId1, userId2);
	}
}